package com.ruoyi.workbench.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.workbench.domain.WbLeafUnit;
import com.ruoyi.workbench.domain.WbWholeUnit;
import com.ruoyi.workbench.mapper.WbLeafUnitMapper;
import com.ruoyi.workbench.service.IWbLeafUnitService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 叶子单元Service业务层处理
 *
 * @author hanjiafeng
 * @date 2021-02-24
 */
@Service
public class WbLeafUnitServiceImpl implements IWbLeafUnitService {
    private final WbLeafUnitMapper wbLeafUnitMapper;
    private final ISysDeptService deptService;

    public WbLeafUnitServiceImpl(WbLeafUnitMapper wbLeafUnitMapper, ISysDeptService deptService) {
        this.wbLeafUnitMapper = wbLeafUnitMapper;
        this.deptService = deptService;
    }

    /**
     * 查询叶子单元
     *
     * @param unitId 叶子单元ID
     * @return 所辖村庄
     */
    @Override
    public WbLeafUnit selectWbLeafUnitById(Long unitId) {
        return wbLeafUnitMapper.selectWbLeafUnitById(unitId);
    }

    @Override
    public WbWholeUnit selectWbWholeUnitById(Long unitId) {
        WbLeafUnit unit7 = wbLeafUnitMapper.selectWbLeafUnitById(unitId);
        SysDept unit6 = deptService.selectDeptById(unit7.getParentId());
        SysDept unit5 = deptService.selectDeptById(unit6.getParentId());
        SysDept unit4 = deptService.selectDeptById(unit5.getParentId());
        SysDept unit3 = deptService.selectDeptById(unit4.getParentId());
        WbWholeUnit wholeUnit = new WbWholeUnit();
        wholeUnit.setUnit3(unit3);
        wholeUnit.setUnit4(unit4);
        wholeUnit.setUnit5(unit5);
        wholeUnit.setUnit6(unit6);
        wholeUnit.setUnit7(unit7);
        return wholeUnit;
    }

    @Override
    public WbWholeUnit selectChannelWbWholeUnitById(Long unitId) {
        WbLeafUnit unit7 = wbLeafUnitMapper.selectWbLeafUnitById(unitId);
        SysDept unit4 = deptService.selectDeptById(unit7.getUnitId());
        SysDept unit3 = deptService.selectDeptById(unit4.getParentId());
        WbWholeUnit wholeUnit = new WbWholeUnit();
        wholeUnit.setUnit3(unit3);
        wholeUnit.setUnit4(unit4);
        wholeUnit.setUnit7(unit7);
        return wholeUnit;
    }

    /**
     * 查询叶子单元列表
     *
     * @param wbLeafUnit 所辖村庄
     * @return 所辖村庄
     */
    @Override
    public List<WbLeafUnit> selectWbLeafUnitList(WbLeafUnit wbLeafUnit) {
        return wbLeafUnitMapper.selectWbLeafUnitList(wbLeafUnit);
    }

    @Override
    public List<WbWholeUnit> selectWbWholeUnitList(WbWholeUnit wholeUnit) {
        return wbLeafUnitMapper.selectWbWholeUnitList(wholeUnit);
    }

    /**
     * 新增叶子单元
     *
     * @param wbLeafUnit 所辖村庄
     * @return 结果
     */
    @Override
    public int insertWbLeafUnit(WbLeafUnit wbLeafUnit) {
        SysUser currentUser = ShiroUtils.getSysUser();
        wbLeafUnit.setCreateTime(DateUtils.getNowDate());
        wbLeafUnit.setCreateBy(currentUser.getUserName());
        return wbLeafUnitMapper.insertWbLeafUnit(wbLeafUnit);
    }

    /**
     * 修改叶子单元
     *
     * @param wbLeafUnit 所辖村庄
     * @return 结果
     */
    @Override
    public int updateWbLeafUnit(WbLeafUnit wbLeafUnit) {
        SysUser currentUser = ShiroUtils.getSysUser();
        wbLeafUnit.setUpdateTime(DateUtils.getNowDate());
        wbLeafUnit.setUpdateBy(currentUser.getUserName());
        return wbLeafUnitMapper.updateWbLeafUnit(wbLeafUnit);
    }

    /**
     * 删除叶子单元对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWbLeafUnitByIds(String ids) {
        return wbLeafUnitMapper.deleteWbLeafUnitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除叶子单元信息
     *
     * @param unitId 叶子单元ID
     * @return 结果
     */
    @Override
    public int deleteWbLeafUnitById(Long unitId) {
        return wbLeafUnitMapper.deleteWbLeafUnitById(unitId);
    }

    @Override
    public List<WbLeafUnit> selectChildrenUnitByParentId(Long parentId) {
        WbLeafUnit wbLeafUnit = new WbLeafUnit();
        wbLeafUnit.setParentId(parentId);
        return wbLeafUnitMapper.selectWbLeafUnitList(wbLeafUnit);
    }
}
